package com.mef.smsplat.dao;



import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import com.cqcis.base.orm.jdbc.BaseJdbcDao;
import com.hc24.util.DateUtil;
import com.mef.smsplat.dao.base.BaseSpringJdbcDao;
import com.mef.smsplat.dao.base.DataGridModel;
import com.mef.smsplat.dao.base.Page;
import com.mef.smsplat.domain.SmsHistoryInfoDomain;
import com.mef.smsplat.web.controller.qo.SmsHistoryQo;


@Repository
public class SmsHistoryInfoDao extends BaseSpringJdbcDao<SmsHistoryInfoDomain, String> {
	
	/**
	 * 根据短信ID查询
	 * @param sReqId
	 * @return
	 */
	public SmsHistoryInfoDomain query(String sReqId){
		String sql="select * from sms_history_info t where t.s_req_id=?";
		List<SmsHistoryInfoDomain> list=super.query(sql, new Object[]{sReqId});
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	
	/**
	 * 分页查询短信发送历史
	 * @param qo 查询条件
	 * @param dgm 分页条件
	 * @return
	 */
	public Page<SmsHistoryInfoDomain> query(SmsHistoryQo qo,DataGridModel dgm){
		List<Object> params=new ArrayList<Object>();
		StringBuilder sql=new StringBuilder();
		sql.append("select * from sms_history_Info t where 1=1");
		if(qo.getBeginDate()!=null){
			sql.append(" and t.send_time>=?");
			params.add(qo.getBeginDate());
		}
		if(qo.getEndDate()!=null){
			sql.append(" and t.send_time<=?");
			params.add(DateUtil.next(qo.getEndDate(), 1));
		}
		if(qo.getFlightNo()!=null&&!"".equals(qo.getFlightNo().trim())){
			sql.append(" and t.flight_no like ?");
			params.add("%"+qo.getFlightNo()+"%");
		}
		if(qo.getPassName()!=null&&!"".equals(qo.getPassName().trim())){
			sql.append(" and t.pass_name = ?");
			params.add(qo.getPassName());
		}
		if(qo.getPnr()!=null&&!"".equals(qo.getPnr().trim())){
			sql.append(" and t.pnr=?");
			params.add(qo.getPnr());
		}
		if(qo.getTktNo()!=null&&!"".equals(qo.getTktNo())){
			sql.append(" and t.tktNo=?");
			params.add(qo.getTktNo());
		}
		if(qo.getStatus()>=0){
			sql.append(" and t.status=?");
			params.add(qo.getStatus());
		}
		sql.append(" order by t.send_time desc");
		return super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params);
	}

	@Override
	public BaseJdbcDao getDaoClass() {
		return this;
	}

	@Override
	public Class getEntityClass() {
		return SmsHistoryInfoDomain.class;
	}

}
